<?php
namespace app\api\controller\customer;

use app\api\controller\Base;
use app\api\controller\BaseUser;
use app\common\model\company\Company;
use app\common\model\user\User;
use think\facade\Request;
use app\common\model\customer\AllocationLog as AllocationLogModel;
use app\common\model\customer\Customer;
use app\api\controller\Pub;

class Allocationlog extends BaseUser
{
	/**
     * 查询分配记录
     */
	public function getLists()
    {
        $params =  Request::instance()->param();
        $where = [];

        if (isset($params['CId']) && $params['CId'] != '') {
            $where['CId'] = $params['CId'];
        }

        if (isset($params['BussinessId']) && $params['BussinessId'] != '') {
            $where['BussinessId'] = $params['BussinessId'];
        }

        // $userModel = new User();
        $data = (new AllocationLogModel())->getLists($where);
        if ($data) {
            $useridlist = [];
            foreach($data as $key=>$value) {
                if(empty($value['OldBussinessId'])){
                    unset($data[$key]);
                    break;
                }
                $useridlist[] = $value['BussinessId'];
                $useridlist[] = $value['OldBussinessId'];
            }
            $useridlist = array_unique($useridlist);
            $userlist = User::where('UserId','in',$useridlist)->column('Name','UserId');
            foreach($data as $key=>$value) {
                if(empty($value['OldBussinessId'])){
                    unset($data[$key]);
                    break;
                }
                // 查询当前业务员
                $data[$key]['BussinessName'] = $userlist[$value['BussinessId']];
                // 查询之前业务员
                $data[$key]['OldBussinessName'] = $userlist[$value['OldBussinessId']];
            }
        }
        if(empty($where)){
            $data=[];
        }
        return myok($data);
    }

    /**
     * 添加分配记录
     */
    public function addAllocationLog()
    {
        $params =  Request::instance()->param();
        // 获取用户信息
        $u_id = $this->user_id;

        $result = $this->validate($params, 'AllocationLog');
        if(true !== $result){
            return myerr($result);
        }
        // 将内容插入
        $res = (new AllocationLogModel())->addAllocationLog($params, $u_id);

        // 修改客户表和公司表业务员id
        // 查询客户
        $customer = (new Customer())->where('CustomerId', $params['CId'])->find();
        $userId=Pub::userid();
        $user=User::get($userId);
        if($user->Role==401){
            //如果是代理商更新当前客户的代理商ID。
            $customer->LatestAgentId=$params['BussinessId'];
        }else{
             $customer->BussinessId  = $params['BussinessId'];
        }
       
        $customer->save();
        if ($customer['CompanyId'] != null) {
            $company = (new Company())->where('CompanyId', $customer['CompanyId'])->find();
            if (!empty($company)) {
                $company->BussinessId     = $params['BussinessId'];
                $company->save();
            }
        }

        if ($res) {
            return myok('操作成功');
        } else {
            return myerr($res);
        }
    }

    // 转移业务员客户
    public function transferCustomer()
    {
        $params =  Request::instance()->param();
        // 获取用户信息
        $u_id = $this->user_id;
        // 查询业务员客户
        $customers = (new Customer())->where('BussinessId', $params['BussinessId'])->select();
        if(count($customers) > 0) {
            foreach($customers as $item) {
                $data = [
                    'CId' => $item['CustomerId'],
                    'BussinessId' => $params['NewBussinessId'],
                    'OldBussinessId' => $params['BussinessId'],
                ];
                // 将内容插入
                (new AllocationLogModel())->addAllocationLog($data, $u_id);

                // 修改客户表和公司表业务员id
                // 查询客户
                $customer = (new Customer())->where('CustomerId', $item['CustomerId'])->find();
                $customer->BussinessId     = $params['NewBussinessId'];
                $customer->save();
                if ($customer['CompanyId'] != null) {
                    $company = (new Company())->where('CompanyId', $customer['CompanyId'])->find();
                    if ($company) {
                        $customer->BussinessId     = $params['NewBussinessId'];
                        $customer->save();
                    }
                }
            }
            return myok('操作成功');
        } else {
            return myerr('业务员没有客户');
        }

    }

}
